﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/

using System;
using System.Collections;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Service.SystemManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.SystemManage
{
    public class SysMenuBusiness
    {
        private readonly SysMenuService _dal = new SysMenuService();

        /// <summary>
        /// 首页加载菜单
        /// </summary>
        /// <returns></returns>
        public string LoadSysMenuList(string loginCode)
        {
            return JsonHelper.Encode(_dal.LoadSysMenuList(loginCode));
        }

        /// <summary>
        /// 获取父节点菜单
        /// </summary>
        /// <returns></returns>
        public string GetSysMenuParentNode()
        {
            var list = _dal.GetSysMenuParentNode();
            var model = new SysMenuEntity { MenuName = "主菜单", MenuID = new Guid("00000000-0000-0000-0000-000000000000"), ParentMenuID = Guid.NewGuid() };
            list.Add(model);
            return JsonHelper.Encode(list);
        }

        /// <summary>
        /// 联动获取子节点
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public string GetSysMenuChildNode(SysMenuQcEntity qcmodel)
        {
            var total = _dal.GetSysMenuChildNodeCount(qcmodel.MenuID);
            var list = _dal.GetSysMenuChildNode(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuId"></param>
        public bool Remove(string menuId)
        {
            return _dal.Remove(menuId);
        }

        /// <summary>
        /// 获取单条菜单信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public string GetSysMenuInfo(string menuId)
        {
            return JsonHelper.Encode(_dal.GetSysMenuInfo(menuId));
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddSysMenu(string data)
        {
            var model = new SysMenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuID = Guid.NewGuid();
                model.MenuName = row["MenuName"].ToString();
                model.URL = row["URL"].ToString();
                model.ParentMenuID = new Guid(row["ParentMenuID"].ToString());
                model.MenuIDX = Convert.ToInt32(row["MenuIDX"].ToString());
                model.Remark = row["Remark"].ToString();
            }
            return _dal.AddSysMenu(model);
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="data"></param>
        public bool ModifySysMenu(string data)
        {
            var model = new SysMenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuID = new Guid(row["MenuID"].ToString());
                model.MenuName = row["MenuName"].ToString();
                model.URL = row["URL"].ToString();
                model.MenuIDX = Convert.ToInt32(row["MenuIDX"].ToString());
                model.Remark = row["Remark"].ToString();
            }
            return _dal.ModifySysMenu(model);
        }

        public string GetSysMenuList()
        {
            return JsonHelper.Encode(_dal.GetSysMenuList());
        }

        public bool AddParentMenuNode(string data)
        {
            var model = new SysMenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuID = Guid.NewGuid();
                model.MenuName = row["MenuName"].ToString();
                model.MenuIDX = Convert.ToInt32(row["MenuIDX"].ToString());
            }
            return _dal.AddParentMenuNode(model);
        }

        public bool ModifyParentMenuNode(string data)
        {
            var model = new SysMenuEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.MenuID = new Guid(row["MenuID"].ToString());
                model.MenuName = row["MenuName"].ToString();
                model.MenuIDX = Convert.ToInt32(row["MenuIDX"].ToString());
            }
            return _dal.ModifyParentMenuNode(model);
        }

        public bool RemoveParentMenuNode(string menuId)
        {
            var result = false;
            if (_dal.Remove(menuId))
            {
                if (_dal.RemoveChildMenuNode(menuId))
                 {
                     result = true;
                 }
            }
            return result;
        }
    }
}
